﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Helper.Core.Entity;
using System.Collections;
using Helper.Business.Implementation;

namespace Helper.Controllers
{
    public partial class BusinessController : BaseController
    {        
        [HttpGet]
        [ActionName("GetAll")]
        public JsonResult GetAll()
        {
            JsonResult result = new JsonResult();
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            IEnumerable dataReturn = _BusinessRepository.GetAll();
            result.Data = dataReturn;
            return result;
        }

        [HttpPost]
        [ActionName("Insert")]
        public JsonResult Insert(Helper.Core.Entity.Business collection)
        {
            try
            {
                _BusinessRepository.Add(collection);
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });                
            }
        }

        [HttpPost]
        [ActionName("Erase")]
        public JsonResult Erase(Helper.Core.Entity.Business collection)
        {
            try
            {
                UserRepository _userRepo = new UserRepository();
                foreach (Core.Entity.User item in _userRepo.FindUsersByBusiness(collection.Id))
                    _userRepo.Delete(item);
                _BusinessRepository.Delete(_BusinessRepository.FindById(collection.Id));
                return Json(new { success = true });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        [HttpPost]
        [ActionName("GetBusiness")]
        public JsonResult GetBusiness(int id)
        {
            try
            {
                Helper.Core.Entity.Business bus = _BusinessRepository.FindById(id);
                return Json(new { success = true, data = bus });                
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        [HttpPost]
        [ActionName("Modify")]
        public JsonResult Modify(int Id, Helper.Core.Entity.Business collection)
        {
            try
            {
                Core.Entity.Business bus = _BusinessRepository.FindById(Id);
                bus.Name = collection.Name;
                _BusinessRepository.Save(bus);
                return Json(new { success = true, data = bus });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

    }
}
